package gui;

import java.awt.BorderLayout;
//import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
//import java.util.Calendar;

//import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
//import javax.swing.JList;
//import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
//import javax.swing.JTextField;
//import javax.swing.JToolBar;
//import javax.swing.event.ListSelectionEvent;
//import javax.swing.event.ListSelectionListener;

import client.Client;

public class ClientChatWindow {
	private JFrame frmClientChatWindow;
	//private JTextField textField;
	private Client client;
	private int roomID;
	private String clientName;
	private ClientMainWindow cmw;
	
	private JButton btnSend;
	private JButton btnInv;
	private JTextArea jta1;
	private JTextArea jta2;
	private JScrollPane jsPane1;
	private JScrollPane jsPane2;
	private JScrollPane jsPane3;
	private JSplitPane jspPane;
	private JSplitPane jspPane2;
	private JSplitPane jspPane3;
	private JSplitPane jspPane4;
	private JList jl;
	
	public static void openWindow(final Client client, final ClientMainWindow cmw, final int ID, final String clientName) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientChatWindow window = new ClientChatWindow(client, cmw, ID, clientName);
					window.frmClientChatWindow.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientChatWindow(Client client, ClientMainWindow cmw, int roomID, String clientName) {
		this.roomID = roomID;
		this.client = client;
		this.clientName = clientName;
		this.cmw = cmw;
		initialize();
		if (client != null)
			client.addChat(roomID, this);
		else
			System.out.println("o kurwa null!");
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frmClientChatWindow = new JFrame();
		frmClientChatWindow.setTitle("Client Chat Window");
		frmClientChatWindow.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (client != null) {
					client.leaveRoom(roomID);
					client.removeChat(roomID);
	
				}
			}
			
		});
		frmClientChatWindow.setBounds(200, 200, 500, 572);
		frmClientChatWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		

		jspPane = new JSplitPane();
		jspPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		jspPane.setDividerLocation(300);
		frmClientChatWindow.getContentPane().add(jspPane, BorderLayout.CENTER);
		
		jspPane3 = new JSplitPane();
		jspPane3.setDividerLocation(400);
		jspPane.setLeftComponent(jspPane3);
		
		jta1 = new JTextArea();
		jta1.setEditable(false);
		jsPane1 = new JScrollPane(jta1);
		jspPane3.setLeftComponent(jsPane1);
		
		jl = new JList(new DefaultListModel());
		jsPane3 = new JScrollPane(jl);
		jspPane3.setRightComponent(jsPane3);
		
		jspPane2 = new JSplitPane();
		jspPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
		jspPane2.setDividerLocation(190);
		jspPane.setRightComponent(jspPane2);
		
		jta2 = new JTextArea();
		jta2.setLineWrap(true);
		jsPane2 = new JScrollPane(jta2);
		jspPane2.setLeftComponent(jsPane2);
		
		jspPane4 = new JSplitPane();
		jspPane2.setRightComponent(jspPane4);
		
		btnSend = new JButton("Send");
		btnSend.setEnabled(false);
		jspPane4.setRightComponent(btnSend);
		
		btnInv = new JButton("Invite");
		btnInv.setEnabled(true);
		jspPane4.setLeftComponent(btnInv);
		
		
		
		
		jta2.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				if (jta2.getText().equals(""))
					btnSend.setEnabled(false);
				else
					btnSend.setEnabled(true);
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if (e.isShiftDown())
						jta2.append("\n");
					else {
						ClientChatWindow.this.updateText();
						e.consume();
					}
				}
			}
		});
		
		btnSend.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ClientChatWindow.this.updateText();
			}
		});
		
		btnInv.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Object[] possibleValues = cmw.getFriends();
				if (possibleValues.length != 0) {
					String selectedValue = (String)JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
					client.sendInviteMessage(selectedValue, roomID);
				}
			}
		});
	}
	
	private void updateText() {
		if (!jta2.getText().equals("")) {
			if(client.sendChatMessage(jta2.getText(), clientName, this.roomID)) {	
				jta1.append(clientName + " :: " + "\n" + jta2.getText() + "\n");
				jta2.setText(null);
			} 
			else {
				JOptionPane.showMessageDialog(null, "You have been logged out", "Chat", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	public void appendMessage(String who, String msg, ArrayList<String> l) {
		jta1.append(who + " :: " + "\n" + msg + "\n");
		jta1.setCaretPosition(jta1.getText().length());
	}
	
	public static void main(String[] args) {
		ClientChatWindow.openWindow(null, null, 0, "Test");
	}
	
	public void updateMembers(final ArrayList<String> l) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				((DefaultListModel)jl.getModel()).removeAllElements();
				
				for(String name : l) {
					((DefaultListModel)jl.getModel()).addElement(name);
					}
				jl.validate();
				jl.repaint();
			}
		});
		/*
		((DefaultListModel)jl.getModel()).removeAllElements();
		
		for(String name : l) {
			((DefaultListModel)jl.getModel()).addElement(name);
			}
		jl.validate();
		jl.repaint();
		*/
	}
	
	public void enable() {
		frmClientChatWindow.setTitle("Client Chat Window");
	}
	
	public void disable() {
		frmClientChatWindow.setTitle("Client Chat Window - disconnected");
	}
	
	public void close() {
		frmClientChatWindow.dispose();
	}
	
	public void showIt() {
		//JOptionPane.showMessageDialog(null, "You have been logged out", "Chat", JOptionPane.ERROR_MESSAGE);
		System.out.println("showIt()");
	}
}
